/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.psp.service.impl;

import me.zhengjie.modules.cspimp.psp.domain.OmSurrounding;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.psp.repository.OmSurroundingRepository;
import me.zhengjie.modules.cspimp.psp.service.OmSurroundingService;
import me.zhengjie.modules.cspimp.psp.service.dto.OmSurroundingDto;
import me.zhengjie.modules.cspimp.psp.service.dto.OmSurroundingQueryCriteria;
import me.zhengjie.modules.cspimp.psp.service.mapstruct.OmSurroundingMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author zmm
* @date 2020-05-30
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class OmSurroundingServiceImpl implements OmSurroundingService {

    private final OmSurroundingRepository omSurroundingRepository;
    private final OmSurroundingMapper omSurroundingMapper;

    @Override
    public Map<String,Object> queryAll(OmSurroundingQueryCriteria criteria, Pageable pageable){
        Page<OmSurrounding> page = omSurroundingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(omSurroundingMapper::toDto));
    }

    @Override
    public List<OmSurroundingDto> queryAll(OmSurroundingQueryCriteria criteria){
        return omSurroundingMapper.toDto(omSurroundingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public OmSurroundingDto findById(Long id) {
        OmSurrounding omSurrounding = omSurroundingRepository.findById(id).orElseGet(OmSurrounding::new);
        ValidationUtil.isNull(omSurrounding.getId(),"OmSurrounding","id",id);
        return omSurroundingMapper.toDto(omSurrounding);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmSurroundingDto create(OmSurrounding resources) {
        resources.setCid(0l);
        return omSurroundingMapper.toDto(omSurroundingRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OmSurrounding resources) {
        OmSurrounding omSurrounding = omSurroundingRepository.findById(resources.getId()).orElseGet(OmSurrounding::new);
        ValidationUtil.isNull( omSurrounding.getId(),"OmSurrounding","id",resources.getId());
        omSurrounding.copy(resources);
        omSurroundingRepository.save(omSurrounding);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            omSurroundingRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<OmSurroundingDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OmSurroundingDto omSurrounding : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建者", omSurrounding.getCreateBy());
            map.put("更新者", omSurrounding.getUpdateBy());
            map.put("创建日期", omSurrounding.getCreateTime());
            map.put("更新时间", omSurrounding.getUpdateTime());
            map.put("单位概述", omSurrounding.getUnitDesc());
            map.put("地理位置与自然条件", omSurrounding.getNaturalPosition());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}